package com.pan.server.controller;

import com.github.pagehelper.PageInfo;
import com.pan.common.result.Result;
import com.pan.pojo.dto.In;
import com.pan.pojo.dto.OrderAddDTO;
import com.pan.pojo.dto.QueryOrdersDTO;
import com.pan.pojo.dto.UpdateOrderDTO;
import com.pan.pojo.vo.OrderVO;
import com.pan.server.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/order")
@Slf4j
@Api(tags = "用户订单控制器")
@RestController
@CrossOrigin("http://localhost:7070")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 查询用户订单
     * @param orderQueryDTO
     * @return
     */
    @PostMapping(value = "/queryOrders")
    @ApiOperation("订单查询")
    public Result<PageInfo<OrderVO>> queryOrders(@RequestBody QueryOrdersDTO orderQueryDTO){
        log.info("用户订单查询...", orderQueryDTO.getUsername());

        PageInfo<OrderVO> pageInfo = ordersService.queryOrders(orderQueryDTO.getUsername(), orderQueryDTO.getPageNum(), orderQueryDTO.getPageSize());

        return Result.success(pageInfo);
    }

    /**
     * 添加订单
     * @param orderAddD
     * @return
     */
    @PostMapping(value = "/addOrder")
    @ApiOperation("订单添加")
    public Result<String> addOrder(@RequestBody OrderAddDTO orderAddD){
        log.info("用户订单添加...", orderAddD.getBookId(), orderAddD.getQuantity());

        ordersService.addOrder(orderAddD.getUsername(), orderAddD.getBookId(), orderAddD.getQuantity());

        return Result.success("订单添加成功");
    }

    /**
     * 更新订单
     * @param updateOrderDTO
     */
    @PostMapping(value = "/updateOrder")
    @ApiOperation("订单更新")
    public Result<String> updateOrder(@RequestBody UpdateOrderDTO updateOrderDTO){
        log.info("用户订单更新...", updateOrderDTO.getOrderId(), updateOrderDTO.getStatus());

        ordersService.updateOrder(updateOrderDTO.getOrderId(), updateOrderDTO.getStatus());

        return Result.success("订单更新成功");
    }



    /**
     * 删除订单
     * @param in
     * @return
     */
    @PostMapping(value = "/deleteOrder")
    @ApiOperation("订单删除")
    public Result<String> deleteOrder(@RequestBody In in) {
        log.info("用户订单删除...", in.getOrderId());

        ordersService.deleteOrder(in.getOrderId());

        return Result.success("订单删除成功");
    }

}